package com.yanqu.road.server.manger;

import com.yanqu.road.entity.servercenter.DAServerInfo;
import com.yanqu.road.logic.bussiness.servercenter.ServerBussiness;
import com.yanqu.road.server.TempMgr;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


public class DAServerListMgr extends TempMgr {

    private static Map<Integer, List<DAServerInfo>> channelServerMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        Map<Integer, List<DAServerInfo>>  channelServerMapTemp = new ConcurrentHashMap<>();
        ServerBussiness.getDAServerInfoList(channelServerMapTemp);
        channelServerMap = channelServerMapTemp;
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static DAServerInfo get(int channelId, long serverId){
        if(!channelServerMap.containsKey(channelId)){
            return null;
        }
        List<DAServerInfo> serverInfoList = channelServerMap.get(channelId);
        for (DAServerInfo serverInfo : serverInfoList) {
            if(serverId == serverInfo.getServerId()){
                return serverInfo;
            }
            if(serverInfo.getServerList().contains(serverId)){
                return serverInfo;
            }
            if(serverInfo.getServerId() > 0 && serverInfo.getEndServerId() > 0 &&
                    serverId >= serverInfo.getServerId() && serverId <= serverInfo.getEndServerId()){
                return serverInfo;
            }
        }
        return null;
    }

}
